package one.bugu.android.demon.ui.activity;

import android.annotation.SuppressLint;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.juefeng.android.framework.LKUtil;
import com.juefeng.android.framework.view.annotation.LKContentView;
import com.juefeng.android.framework.view.annotation.LKInjectView;

import java.util.HashMap;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import one.bugu.android.demon.R;
import one.bugu.android.demon.common.BaseHttpAsycResponceHandler;
import one.bugu.android.demon.common.MyBaseActivity;
import one.bugu.android.demon.constant.Constant;
import one.bugu.android.demon.constant.HttpConstant;
import one.bugu.android.demon.ui.widget.BaseTopBarView;
import one.bugu.android.demon.util.PreferencesUtil;
import one.bugu.android.demon.util.StatusBarUtils;

/**
 * 意见反馈界面
 */
@LKContentView(R.layout.activity_feedback)
public class FeedbackActivity extends MyBaseActivity{

    /**
     * topBar
     */
    @LKInjectView(R.id.btbv_feed_back)
    private BaseTopBarView baseTopBarView;

    /**
     * 编辑框输入字数显示
     */
    @LKInjectView(R.id.opinionNum)
    private TextView opinionNum;

    /**
     * 意见反馈编辑框
     */
    @LKInjectView(R.id.edt_opinion)
    private EditText edtOpinion;

    /**
     * 提交反馈内容按钮
     */
    @LKInjectView(R.id.opinion_save)
    private Button opinionSave;

    String token;

    @Override
    protected void initView() {
        super.initView();

        StatusBarUtils.getInstance().setStatusBar("#EEEEEE", this, baseTopBarView, false);
        //获取token
        token = PreferencesUtil.getInstance().getString(FeedbackActivity.this, Constant.TOKEN);

    }

    @Override
    protected void initData() {
        super.initData();
    }

    /**
     * 提交意见反馈方法
     */
    public void saveOpinion(){

        Map<String, Object> map = new HashMap<>();
        map.put("token", token);
        map.put("docTitle", edtOpinion.getText().toString());

        LKUtil.getHttpManager().postBody(HttpConstant.SAVEFK_URL, map, handler);
    }

    /**
     * 提交意见反馈网络请求
     */
    @SuppressLint("HandlerLeak")
    public BaseHttpAsycResponceHandler<String> handler = new BaseHttpAsycResponceHandler<String>(true){

        @Override
        public void onSuccess(String data) {
            super.onSuccess(data);

            Toast.makeText(FeedbackActivity.this, "提交成功", Toast.LENGTH_SHORT).show();
            finish();
        }

        @Override
        public void onFailed(int code, String message) {
            super.onFailed(code, message);

        }

        @Override
        public void onError() {
            super.onError();
        }
    };

    @Override
    protected void initEvent() {
        super.initEvent();

        edtOpinion.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            /**
             * 监听输入框
             * @param charSequence
             * @param i
             * @param i1
             * @param i2
             */
            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {

                String editable = edtOpinion.getText().toString();
                String regEx = "[^\u4E00-\u9FA5]";  //只能输入字母或数字
                Pattern p = Pattern.compile(regEx);
                Matcher m = p.matcher(editable);
                String str = m.replaceAll("").trim();    //删掉不是字母或数字的字符
                if(!editable.equals(str)){
                    edtOpinion.setText(str);  //设置EditText的字符
                    edtOpinion.setSelection(str.length()); //因为删除了字符，要重写设置新的光标所在位置
                    opinionNum.setText(edtOpinion.length() + "/500");
                }
                //根据编辑框输入文字数量显示文字
                opinionNum.setText(edtOpinion.length() + "/500");
            }

            @Override
            public void afterTextChanged(Editable editable) {

            }
        });

        //返回
        baseTopBarView.setEventInterface(new BaseTopBarView.EventInterface() {
            @Override
            public void leftOnClick() {
                finish();
            }

            @Override
            public void leftCloseClick() {

            }

            @Override
            public void rightOnClick() {

            }
        });
        //提交点击事件
        opinionSave.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                //条件限制  不能为空 不能少于10个字
                if (edtOpinion.getText().length() < 10){

                    Toast.makeText(FeedbackActivity.this, "不能少于10个字", Toast.LENGTH_SHORT).show();
                }else if (edtOpinion.getText().length() == 0){

                    Toast.makeText(FeedbackActivity.this, "内容不能为空", Toast.LENGTH_SHORT).show();
                }else {

                    saveOpinion();
                }
            }
        });
    }
}
